package com.project8.book;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

import android.os.Environment;

public class notebookFileStuff 
{
	//This function runs when the notebook is made visible. It reads the contents of the notebook file and displays
	public static String readContents() throws IOException	
	{
		StringBuffer contents = new StringBuffer();
		String da = "Nothing Happened Yet...";
		
		//Checks if SD Card is available Then saves the file
		String exStorageState = Environment.getExternalStorageState();
		if (Environment.MEDIA_MOUNTED.equals(exStorageState))
		{
			File root = Environment.getExternalStorageDirectory();
			String path = root+"/tmp/";
			
			boolean exists = (new File(path).exists());
			if (!exists) {new File(path).mkdirs();}
			
			File file = new File(path+"notes.txt");
			file.createNewFile();
			
			BufferedReader reader = null;
			
			//Read the file line by line and a to the string buffer contents
			try 
			{
				reader = new BufferedReader(new FileReader(file));
				String text = null;
				// repeat until all lines is read
				while ((text = reader.readLine()) != null) 
				{
					contents.append(text)
					.append(System.getProperty(
					"line.separator"));
				}
			} 
			catch (FileNotFoundException e) 
			{
				e.printStackTrace();
			} 
			catch (IOException e) 
			{
				e.printStackTrace();
			} 
			finally 
			{
				try 
				{
					if (reader != null) 
					{
						reader.close();
					}
				} 
				catch (IOException e) 
				{
					e.printStackTrace();
				}
			}
			// show file contents here
			System.out.println(contents.toString());
		}
		else
		{//SD Card not supported
			//FAIL
			System.out.println("File not accessible");
		}
		
		da = contents.toString();
		return da;
	}
	
	//This function saves the content of the notebook on a file (Overwrites previous contents).
	public static void writeContents(String source) 
	{
		String exStorageState = Environment.getExternalStorageState();
		Writer writer = null;
		
		if (Environment.MEDIA_MOUNTED.equals(exStorageState))
		{
			try 
			{//Write the file
				File root = Environment.getExternalStorageDirectory();
				String path = root+"/tmp/";
				
				boolean exists = (new File(path).exists());
				
				if (!exists) {new File(path).mkdirs();}
					
				File file = new File(path+"notes.txt");
				file.createNewFile();
				
				writer = new BufferedWriter(new FileWriter(file));
				writer.write(source);
			} 
			catch (FileNotFoundException e) 
			{
				e.printStackTrace();	
			} 
			catch (IOException e) 
			{
				e.printStackTrace();
			} 
			finally 
			{
				try 
				{
					if (writer != null) 
					{
						writer.close();
					}
				} 
				catch (IOException e) 
				{
					e.printStackTrace();
				}
			}
		}
		else
		{
			//FAIL
			System.out.println("File not accessible");
		}
	}
}